package com.sdy.dcsb.web.controller.inboundservice;

import com.sdy.dcsb.web.controller.inboundservice.util.ProcessorUtil;
import com.sdy.dcsb.web.controller.inboundservice.util.ServiceUtil;
import com.sdy.dcsb.web.controller.inboundservice.util.ServletUtil;
import com.sdy.mvc.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.xml.soap.MimeHeaders;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import static org.springframework.util.StringUtils.isEmpty;

/**
 * <p>
 * http类接口
 * </p>
 *
 * @author fb
 * @since 2019/10/28
 */
@Slf4j
@Controller
@RequestMapping("/DCSBWeb/servlets")
@Api(tags = "http类接口")
public class HttpServiceController extends BaseController {

    @Autowired
    private ProcessorUtil processorUtil;

    /**
     * 获取请求的接口编码和版本
     * @param code ,编码和版本
     * @return
     */
    @RequestMapping("/{code}")
    @ResponseBody
    @ApiOperation("获取请求的接口编码和版本")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "code", value = "编码和版本"),
    })
    public String getCode(HttpServletRequest request, @PathVariable("code") String code) throws IOException{
        long requestTime = System.currentTimeMillis();
        String responseMsg = null;
        String servcode = null;
        String servversion = null;
        String requestIp = null;
        String requestHttpType = null;
        String queryString = null;
        MimeHeaders headers = null;
        HashMap<String, String> map = new HashMap<String, String>();
        String serviceUrl = request.getRequestURL().toString();
        String address = serviceUrl.substring(serviceUrl.indexOf("/")+2);
        String[] array = address.split("/");
        // 获取请求url中的ip+端口
        String ipAndPort = array[0];
        if(!isEmpty(code)) {
            Map<String, String> servcodeMap = ServiceUtil.getServcode(code);
            servcode = servcodeMap.get("servcode");
            servversion = servcodeMap.get("servversion");
            requestIp = ServletUtil.getIpAddr(request);
            requestHttpType = request.getMethod();
            queryString = request.getQueryString();
            Map<String, String> queryStringMap = ServiceUtil.getQueryString(queryString, responseMsg);
            if(queryStringMap.containsKey("code")) {
                return queryStringMap.get("responseMsg");
            }else {
                queryString = queryStringMap.get("queryString");
            }
            map = ServletUtil.headerMap(request);
            headers = ServletUtil.getHeaders(request);
            String[] reqLengths = headers.getHeader("Content-Length");
            long reqLength = 0;
            if(null != reqLengths && reqLengths.length > 0) {
                reqLength = Integer.parseInt(reqLengths[0]);
            }
            // 获取请求的字符集
            String reqCharset = null;
            String contentType = null;
            contentType = request.getContentType();
            Map<String, String> contentTypeMap = ServiceUtil.getContentType(contentType, request);
            contentType = contentTypeMap.get("contentType");
            reqCharset = contentTypeMap.get("reqCharset");
            String requestMsg = "";
            // 表单http请求
            Map<String, String> requestMsgMap = ServiceUtil.getRequestMsg(contentType, request, reqCharset);
            if(requestMsgMap.containsKey("code")) {
                return requestMsgMap.get("responseMsg");
            }else {
                requestMsg = requestMsgMap.get("requestMsg");
            }
            // 把头信息放到queryString里
            if(isEmpty(queryString)) {
                queryString = contentType;
            }
            if(reqLength == 0) {
                reqLength = requestMsg.length();
            }
            if(!queryString.equals(contentType)){
                reqLength = reqLength + queryString.length();
            }
            // 访问标准服务
            responseMsg = processorUtil.doProcess(servcode, servversion, requestIp, requestHttpType, Integer.valueOf(String.valueOf(reqLength)),
                    queryString, contentType, requestMsg, reqCharset, requestTime, "2", ipAndPort, map, null);
            return responseMsg;
        }else {
            responseMsg = "Error url, please check !";
            return responseMsg;
        }
    }
}
